import React from 'react';
import { Modal, Button, Form, Input, Switch } from 'antd';

const Item = Form.Item;
const TextArea = Input.TextArea;

@Form.create()
class Components extends React.Component {
  /**
   * 提交表单到父组件
   */
  onOk = () => {
    this.props.form.validateFieldsAndScroll((err, values) => {
      if (!err) {
        values.state = values.state ? 1 : 0;
        this.props.submit(values);
      }
    });
  };
  clear = () => {
    this.props.form.resetFields();
  };

  render() {
    const {
      visible,
      formatData,
      close,
      form: { getFieldDecorator },
    } = this.props;

    const formItemLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 8 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 12 },
      },
    };
    return (
      <Modal
        title="管理设备组"
        afterClose={this.clear}
        visible={visible}
        onCancel={close}
        okText="确定"
        cancelText="取消"
        onOk={this.onOk}
      >
        <Form>
          <Item {...formItemLayout} label="邮箱地址">
            {getFieldDecorator('email', {
              rules: [
                { required: true, message: '不能为空' },
                { type: 'email', message: '邮箱格式有误' },
              ],
              initialValue: formatData && formatData.email,
            })(<Input />)}
          </Item>
          <Item {...formItemLayout} label="启用状态">
            {getFieldDecorator('state', {
              valuePropName: 'checked',
              initialValue: formatData && formatData.state ? true : false,
            })(<Switch checkedChildren="开" unCheckedChildren="关" />)}
          </Item>
          <Item {...formItemLayout} label="备注">
            {getFieldDecorator('remark', {
              initialValue: formatData && formatData.remark,
            })(<Input />)}
          </Item>
        </Form>
      </Modal>
    );
  }
}

export default Components;
